package com.zfy.tingshu.search;

import com.rabbitmq.client.Channel;
import com.zfy.tingshu.common.rabbit.constant.MqConst;
import com.zfy.tingshu.search.service.SearchService;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



/**
 * @author 86188
 * @version v1.0
 * @project tingshu-parent
 * @Date 2025/9/5 16:33
 * @company []
 * @description
 */
@Component
public class SearchReceiver {
    @Autowired
    private SearchService searchService;


    /**
     * 专辑上架
     * @param albumId
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER,durable = "true"),
            key = {MqConst.ROUTING_ALBUM_UPPER}
    ))
    public void upperGoods(Long albumId, Message message, Channel channel){
        //业务处理
        if (null!=albumId){
            searchService.upperAlbum(albumId);
        }
        //手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    /**
     * 专辑下架
     * @param albumId
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER,durable = "true"),
            key = {MqConst.ROUTING_ALBUM_LOWER}
    ))
    public void lowerGoods(Long albumId, Message message, Channel channel){
        //业务处理
        if (null!=albumId){
            searchService.lowerAlbum(albumId);
        }
        //手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
